package com.suez.eimsserver.controller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.suez.eimsserver.entity.Department;
import com.suez.eimsserver.impl.IDepartment;
import com.suez.eimsserver.server.DbUtil;

public class DepartmentController implements IDepartment{

	@Override
	public List<Department> queryAll() {
		// TODO Auto-generated method stub
		List<Department> list = new ArrayList<Department>();
		Connection connection = DbUtil.getConnection();
		PreparedStatement pst = null ;
		ResultSet rs = null;
		
		String sql = "select * from dept";
		try {
			pst= connection.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				String deptId = rs.getString(1);
				double sal = Double.parseDouble(rs.getString(2));
				String dname = rs.getString(3);
				String city = rs.getString(4);
				Department department = new Department(deptId,sal,dname, city);
				list.add(department);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbUtil.freeRS(rs);
			DbUtil.freePre(pst);
			DbUtil.freeConn(connection);
		}
		
		return list;

	}

	@Override
	public int insert(Department dept) {
		// TODO Auto-generated method stub
		Connection connection = DbUtil.getConnection();
		PreparedStatement pst = null;
		String deptId = dept.getDeptId();
		double sal = dept.getSal();
		String dname = dept.getdName();
		String city = dept.getCity();
		
		String sql = "insert into dept values ('"+deptId+"',"+sal+",'"+dname+"','"+city+"')";
		System.out.println("sql 语句"+sql);
		int flag=0;
		try {
			pst = connection.prepareStatement(sql);
			flag = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbUtil.freePre(pst);
			DbUtil.freeConn(connection);
		}
		
		return flag;
	}

	@Override
	public int delete(String deptId) {
		// TODO Auto-generated method stub
		Connection connection = DbUtil.getConnection();
		PreparedStatement pst = null;
		
		String sql ="delete dept where deptid = '"+deptId+"'";
		System.out.println("sql 语句"+sql);
		int flag=0;
		try {
			pst = connection.prepareStatement(sql);
			flag = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbUtil.freePre(pst);
			DbUtil.freeConn(connection);
		}
		
		return flag;
	}
	@Override
	public int update(Map<String, Object> msg) {
		// TODO Auto-generated method stub
		Connection connection = DbUtil.getConnection();
		PreparedStatement pst = null;
		String sql = getUpdateStr(msg);
		System.out.println("sql 语句"+sql);
		
		int flag = 0;
		try {
			pst = connection.prepareStatement(sql);
			flag = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbUtil.freePre(pst);
			DbUtil.freeConn(connection);
		}
		
		
		return flag;
	}
//	@Override
//	public int update(String deptId, String message) {
//		// TODO Auto-generated method stub
//		Connection connection = DbUtil.getConnection();
//		PreparedStatement pst = null;
//		String sql = getUpdateStr(deptId, splitMsg(message));
//		
//		int flag = 0;
//		try {
//			pst = connection.prepareStatement(sql);
//			flag = pst.executeUpdate();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			DbUtil.freePre(pst);
//			DbUtil.freeConn(connection);
//		}
//		
//		
//		return flag;
//	}
	
//	public Map<String, String> splitMsg(String message){
//		Map<String, String> msg = new HashMap<String, String>();
//		String arr[] = message.split(",");
//		for(String temp:arr){
//			msg.put(temp.split(":")[0], temp.split(":")[1]);
//		}
//		
//		return msg;
//	}
//	
	public String getUpdateStr(Map<String, Object> msg){
		//update TEACHERS set teacher_name='艾玛' where teacher_id ='010';
		String sql="update dept set ";
		int flag=0;
		String id = (String)msg.get("deptid");
		if(msg.get("sal")!=null){
			double temp =(double)msg.get("sal");
			sql = sql + "sal="+temp;
			flag++;
		}
		if(msg.get("dname")!=null){
			if(flag>0){
				sql = sql +" , ";
			}
			String dname = (String) msg.get("dname");
			sql = sql + "dname='"+dname+"'";
			flag++;
		}
		if(msg.get("city")!=null){
			if(flag>0){
				sql = sql + " , ";
			}
			String city = (String)msg.get("city");
			sql = sql + "city='"+city+"'";
		}
		sql = sql + " where deptid = '"+id+"'";
		return sql;
	}

	
	

}
